﻿Public Class frmMain


#Region "Form events"

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            Dim ctl As Control
            Dim ctlMDI As MdiClient

            ' Loop through all of the form's controls looking
            ' for the control of type MdiClient.
            For Each ctl In Me.Controls
                Try
                    ' Attempt to cast the control to type MdiClient.
                    ctlMDI = CType(ctl, MdiClient)

                    ' Set the BackColor of the MdiClient control.
                    ctlMDI.BackColor = Me.BackColor

                Catch exc As InvalidCastException
                    ' Catch and ignore the error if casting failed.
                End Try
            Next

        Catch ex As Exception
            Base.ShowError.Show("Main", Me.Name, "Form1_Load", ex)
        End Try
    End Sub

#End Region

#Region "Other functions"

    Private Function Find(ByVal inNameForm As String, ByVal inTextForm As String) As Form
        Try
            Dim elencoForm = From lForm As Form In Me.MdiChildren
                    Where lForm.Name = inNameForm And IIf(inTextForm <> "", lForm.Text = inTextForm, True)
            If elencoForm.Count > 0 Then
                Return elencoForm(0)
            Else
                Return Nothing
            End If
        Catch ex As Exception
            Base.ShowError.Show("Find", Me.Name, "Find", ex)
            Return Nothing
        End Try
    End Function

#End Region

#Region "Tables"

#Region "TablesSingle"

    Private Sub OpenTablesSingleClick(ByVal tableType As Data.TableSingleType)
        Dim oTableSingleInfo As New Data.TableSingleInfo
        oTableSingleInfo.intType = tableType

        Try
            Dim oForm As frmTableSingleList = Nothing
            oForm = Me.Find(frmTableSingleList.Name, oTableSingleInfo.strNameForm)
            If oForm Is Nothing Then
                oForm = New frmTableSingleList
                oForm.MdiParent = Me
                If oForm.Initialize(oTableSingleInfo) Then
                    oForm.Show()
                End If
            Else
                oForm.BringToFront()
            End If

            oForm = Nothing
        Catch ex As Exception
            Base.ShowError.Show("Table " & oTableSingleInfo.strNameTable, Me.Name, "OpenTablesSingleClick", ex)
        End Try
    End Sub

    Private Sub cmdTopics_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdTopics.Click
        Call OpenTablesSingleClick(Data.TableSingleType.TOPICS)
    End Sub

    Private Sub cmdBacklogs_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdBacklogs.Click
        Call OpenTablesSingleClick(Data.TableSingleType.BACKLOGS)
    End Sub

    Private Sub cmdActivities_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdActivities.Click
        Call OpenTablesSingleClick(Data.TableSingleType.ACTIVITIES)
    End Sub

#End Region
    
    Private Sub cmdMeetings_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdMeetings.Click
        Try
            Dim ofrmMeetingElenco As frmMeetingsList = Nothing
            ofrmMeetingElenco = Me.Find(frmMeetingsList.Name, "")
            If ofrmMeetingElenco Is Nothing Then
                ofrmMeetingElenco = New frmMeetingsList
                ofrmMeetingElenco.MdiParent = Me
                If ofrmMeetingElenco.Initialize() Then
                    ofrmMeetingElenco.Show()
                End If
            Else
                ofrmMeetingElenco.BringToFront()
            End If

            ofrmMeetingElenco = Nothing
        Catch ex As Exception
            Base.ShowError.Show("Table Meetings", Me.Name, "cmdMeetings_Click", ex)
        End Try
    End Sub

    Private Sub cmdProjects_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdProjects.Click
        Try
            Dim ofrmProjectsList As frmProjectsList = Nothing
            ofrmProjectsList = Me.Find(frmProjectsList.Name, "")
            If ofrmProjectsList Is Nothing Then
                ofrmProjectsList = New frmProjectsList
                ofrmProjectsList.MdiParent = Me
                If ofrmProjectsList.Initialize() Then
                    ofrmProjectsList.Show()
                End If
            Else
                ofrmProjectsList.BringToFront()
            End If

            ofrmProjectsList = Nothing
        Catch ex As Exception
            Base.ShowError.Show("Table Projects", Me.Name, "cmdProjects_Click", ex)
        End Try
    End Sub

    Private Sub cmdUserStories_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdUS.Click
        Try
            Dim ofrmUserStoriesList As frmUserStoriesList = Nothing
            ofrmUserStoriesList = Me.Find(frmUserStoriesList.Name, "")
            If ofrmUserStoriesList Is Nothing Then
                ofrmUserStoriesList = New frmUserStoriesList
                ofrmUserStoriesList.MdiParent = Me
                If ofrmUserStoriesList.Initialize() Then
                    ofrmUserStoriesList.Show()
                End If
            Else
                ofrmUserStoriesList.BringToFront()
            End If

            ofrmUserStoriesList = Nothing
        Catch ex As Exception
            Base.ShowError.Show("Table UserStories", Me.Name, "cmdUserStories_Click", ex)
        End Try
    End Sub

    Private Sub cmdSprints_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSprints.Click
        Try
            Dim ofrmSprintsList As frmSprintsList = Nothing
            ofrmSprintsList = Me.Find(frmSprintsList.Name, "")
            If ofrmSprintsList Is Nothing Then
                ofrmSprintsList = New frmSprintsList
                ofrmSprintsList.MdiParent = Me
                If ofrmSprintsList.Initialize() Then
                    ofrmSprintsList.Show()
                End If
            Else
                ofrmSprintsList.BringToFront()
            End If

            ofrmSprintsList = Nothing
        Catch ex As Exception
            Base.ShowError.Show("Table Sprints", Me.Name, "cmdSprints_Click", ex)
        End Try
    End Sub

    Private Sub cmdTasks_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdTasks.Click
        Try
            Dim ofrmTasks As frmTasks = Nothing
            ofrmTasks = Me.Find(frmTasks.Name, "")
            If ofrmTasks Is Nothing Then
                ofrmTasks = New frmTasks
                ofrmTasks.MdiParent = Me
                If ofrmTasks.Initialize() Then
                    ofrmTasks.Show()
                End If
            Else
                ofrmTasks.BringToFront()
            End If

            ofrmTasks = Nothing
        Catch ex As Exception
            Base.ShowError.Show("Table Tasks", Me.Name, "cmdTasks_Click", ex)
        End Try
    End Sub

#End Region

#Region "Gestione timer"

    Private Sub trmMain_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles trmMain.Tick
        Dim intMeetingID As Integer = Data.TMeetings.GetMeetingID(Now.Date, Now.TimeOfDay.ToString.Substring(0, 5))
        If intMeetingID > 0 Then
            Dim ofrmAlert As New frmAlert
            ofrmAlert.Initialize(intMeetingID)
            ofrmAlert.ShowDialog()
            ofrmAlert = Nothing
        End If
    End Sub

#End Region
    
End Class
